package com.csiec.clock.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.csiec.clock.R;
import com.csiec.clock.application.MyApplication;
import com.csiec.clock.bean.UserInfo;
import com.csiec.clock.databinding.ActivityUserInfoBinding;
import com.csiec.clock.loacltion.LocationService;
import com.csiec.clock.view.model.UserInfoViewModel;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.listener.multi.CompositeMultiplePermissionsListener;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

public class UserInfoActivity extends AppCompatActivity {

    private ActivityUserInfoBinding binding;
    private UserInfoViewModel userInfoViewModel;

    private String faceInfo;

    private String TAG = UserInfoActivity.class.getSimpleName();
    private LocationService locationService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //接受Intent传来的数据
        Intent intent = getIntent();
        faceInfo = (String) intent.getSerializableExtra("faceInfo");

        binding = DataBindingUtil.setContentView(this, R.layout.activity_user_info);
        //livedata
        userInfoViewModel = new ViewModelProvider(this).get(UserInfoViewModel.class);
        binding.setUserInfoViewModel(userInfoViewModel);
        binding.setLifecycleOwner(this);

        if (faceInfo == null) {
            faceInfo = "";
        }
        userInfoViewModel.init(this, binding, faceInfo);

        //键盘弹起时，布局不会被顶上去
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
        // 获取定位权限
        getPermissions();

    }

    /**
     * Android 从6.0 开始要求一些权限在动态运行时申请
     * 单纯的在 AndroidManifest.xml 注册不起作用
     */
    @TargetApi(23)
    private void getPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Dexter.withActivity(this)
                    .withPermissions(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)
                    .withListener(new CompositeMultiplePermissionsListener())
                    .check();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        // -----------location config ------------
        locationService = ((MyApplication) getApplication()).locationService;
        //获取locationservice实例，建议应用中只初始化1个location实例，然后使用，可以参考其他示例的activity，都是通过此种方式获取locationservice实例的
        locationService.registerListener(mListener);
        //注册监听
        int type = getIntent().getIntExtra("from", 0);
        if (type == 0) {
            LocationService.setLocationOption(locationService.getDefaultLocationClientOption());
        } else if (type == 1) {
            locationService.start();
        }

        binding.dingwei.setOnClickListener(v -> {
            showTip(QMUITipDialog.Builder.ICON_TYPE_LOADING, "获取定位中");
            locationService.start();// 定位SDK
        });
    }

    /***
     * Stop location service
     */
    @Override
    protected void onStop() {
        locationService.unregisterListener(mListener); //注销掉监听
        locationService.stop(); //停止定位服务
        super.onStop();
    }

    /**
     * 定位结果回调，重写onReceiveLocation方法，可以直接拷贝如下代码到自己工程中修改
     */
    private final BDAbstractLocationListener mListener = new BDAbstractLocationListener() {
        /**
         * 定位请求回调函数
         * @param location 定位结果
         */
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (null != location && location.getLocType() != BDLocation.TypeServerError) {
//                binding.textView32.setText(location.getAddrStr());
//                Log.d(TAG, "纬度: " + location.getLatitude());// 纬度
//                Log.d(TAG, "经度: " + location.getLongitude());// 经度
                UserInfo.getUserInfo().setAddress(binding.textView32.getText().toString());
                UserInfo.getUserInfo().setPosition(location.getLatitude() + "," + location.getLongitude());
                userInfoViewModel.setPosition(location.getAddrStr());
            }
        }
    };

    //提示悬浮窗
    public void showTip(int type, String msg) {
        QMUITipDialog tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(type)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        binding.textView32.postDelayed(tipDialog::dismiss, 1000);
    }
}